<?php
include '../app/common.php';
include '../check.php';

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>文字广告</title>
    <link rel="stylesheet" href="../component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">广告位别名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="nickname" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">广告标题</label>
                    <div class="layui-input-inline">
                        <input type="text" name="ad_title" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">广告链接</label>
                    <div class="layui-input-inline">
                        <input type="text" name="ad_url" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="user-enable">
    <input type="checkbox" name="is_show" value="{{d.id}}" lay-skin="switch" lay-text="展示|关闭" lay-filter="user-enable" {{ d.is_show== 1 ? 'checked' : '' }} />
</script>


<script type="text/html" id="startTime">
    {{layui.util.toDateString(d.start_time, 'yyyy-MM-dd')}}
</script>

<script type="text/html" id="endTime">
    {{layui.util.toDateString(d.end_time, 'yyyy-MM-dd')}}
</script>
<script type="text/html" id="status">
    {{#if (layui.util.toDateString(d.end_time, 'yyyy-MM-dd') >=  layui.util.toDateString(d.today, 'yyyy-MM-dd')) { }}
    <span style="color: #5bbd75"><b>正常</b></span>
    {{# }else { }}
    <span style="color: #BD362F"><b>到期</b></span>
    {{# } }}
</script>

<script src="../component/layui/layui.js"></script>
<script src="../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery','common'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let MODULE_PATH = "./";

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '广告位别名',
                    field: 'nickname',
                    align: 'center',
                    width: 130
                },
                {
                    title: '排序',
                    field: 'sort',
                    align: 'center'
                },
                {
                    title: '广告商QQ',
                    field: 'ad_qq',
                    align: 'center',
                    width: 150
                },
                {
                    title: '广告位标题',
                    field: 'ad_title',
                    align: 'center',
                    width: 180
                },
                {
                    title: '广告链接',
                    width: 150,
                    field: 'ad_url',
                    align: 'center'
                },
                {
                    title: '状态',
                    align: 'center',
                    templet: '#status',
                    width: 60,
                },
                {
                    title: '展示状态',
                    align: 'center',
                    templet: '#user-enable',
                    width: 120,
                },
                {
                    title: '开始时间',
                    field: 'start_time',
                    align: 'center',
                    templet: '#startTime',
                    width: 120
                },
                {
                    title: '结束时间',
                    field: 'end_time',
                    align: 'center',
                    templet: '#endTime',
                    width: 120
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 130
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: 'ajax.class.php?act=getWordAdData',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter']
        });

        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
            })
            return false;
        });


        form.on('switch(user-enable)', function(data) {
            var is_show = data.elem.checked ? 1 : 0;
            var id = this.value;
            var load = layer.load();
            $.post('ajax.class.php?act=changeWordAdIsShow', {is_show: is_show, id: id}, function (res) {
                layer.close(load);
                //判断有没有权限
                if (res.code == 1) {
                    layer.msg(res.msg, {icon: 1, time: 1500})
                } else {
                    layer.msg(res.msg, {icon: 2, time: 1500}, function () {
                        $(data.elem).prop('checked', !$(data.elem).prop('checked'));
                        form.render()
                    })
                }
            })
        });


        window.add = function() {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: [common.isModile()?'100%':'600px', common.isModile()?'100%':'500px'],
                content: MODULE_PATH + 'word_ad_add.php'
            });
        }

        window.edit = function(obj) {
            layer.open({
                type: 2,
                title: '修改编号:'+obj.data.id,
                shade: 0.1,
                area: [common.isModile()?'100%':'600px', common.isModile()?'100%':'500px'],
                content: MODULE_PATH + 'word_ad_edit.php?id='+obj.data.id
            });
        }

        window.remove = function(obj) {
            layer.confirm('确定要删除该数据', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "ajax.class.php?act=removeWordAd&id=" + obj.data['id'],
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function(obj) {

            var checkIds = common.checkField(obj,'id');

            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除这些数据', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "ajax.class.php?act=batchRemoveWordAd&ids=" + checkIds,
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
